//package com.fs.client.controller;
//
//import java.net.URI;
//
//import com.fs.client.commons.Result;
//import com.fs.client.dto.GatewayRouteDefinition;
//import com.fs.client.service.impl.DynamicRouteService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.util.UriComponentsBuilder;
//
//@RestController
//@RequestMapping("/route")
//public class RouteController {
//
//    @Autowired
//    private DynamicRouteService dynamicRouteService;
//
//    /**
//     * 初始化 转化对象
//     */
//    private static MapperFacade routeDefinitionMapper;
//    static {
//        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
//        mapperFactory.classMap(GatewayRouteDefinition.class,RouteDefinition.class)
//                .exclude("uri")
//                .byDefault();
//        routeDefinitionMapper = mapperFactory.getMapperFacade();
//    }
//
//    //增加路由
//    @PostMapping("/add")
//    public Result add(@RequestBody GatewayRouteDefinition gatewayRouteDefinition) {
//        try {
//            RouteDefinition definition = transformFo(gatewayRouteDefinition);
//            dynamicRouteService.add(definition);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return Result.succeed("");
//    }
//
//    //删除路由
//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable String id) {
//        return Result.succeed(dynamicRouteService.delete(id));
//    }
//
//
//    //更新路由
//    @PostMapping("/update")
//    public Result update(@RequestBody GatewayRouteDefinition gatewayRouteDefinition) {
//        try {
//            RouteDefinition definition = transformFo(gatewayRouteDefinition);
//            dynamicRouteService.update(definition);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return Result.succeed("");
//    }
//
//
//    /**
//     * 转化路由对象 因为存在 uri 需要转化
//     * @param gatewayRouteDefinition
//     * @return
//     */
//    private RouteDefinition transformFo(GatewayRouteDefinition gatewayRouteDefinition){
//        RouteDefinition definition = new RouteDefinition();
//        routeDefinitionMapper.map(gatewayRouteDefinition,definition);
//        //设置路由id
//        if (!StringUtils.isNotBlank(definition.getId())){
//            definition.setId(java.util.UUID.randomUUID().toString().toUpperCase().replace("-",""));
//        }
//
//        URI uri = null;
//        if(gatewayRouteDefinition.getUri().startsWith("http")){
//            uri = UriComponentsBuilder.fromHttpUrl(gatewayRouteDefinition.getUri()).build().toUri();
//        }else{
//            // uri为 lb://consumer-service 时使用下面的方法
//            uri = URI.create(gatewayRouteDefinition.getUri());
//        }
//        definition.setUri(uri);
//        return definition;
//    }
//
//
//}
